package com.zb.frame.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.frame.base.model.MPPage;
import com.zb.frame.modules.sys.entity.ParamList;
import com.zb.frame.modules.sys.entity.ParamType;
import com.zb.frame.modules.sys.mapper.ParamListMapper;
import com.zb.frame.modules.sys.service.ParamListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bangZZ
 * @since 2020-09-07
 */
@Service
public class ParamListServiceImpl extends ServiceImpl<ParamListMapper, ParamList> implements ParamListService {

    @Override
    public MPPage getParams(Page<ParamList> pageInfo, Map<String, Object> params) {
        QueryWrapper<ParamList> wrapper = new QueryWrapper();
        wrapper.eq("type_id", params.get("id"));
        if (StringUtils.isNotBlank((String)params.get("name"))) {
            wrapper.and(qw -> qw.like("pa_key", params.get("name")).or().like("pa_desc", params.get("name")));
        }
        wrapper.orderByDesc("create_time");
        IPage<ParamList> page = this.page(pageInfo, wrapper);
        return new MPPage(page);
    }

    @Override
    public boolean isExist(String key) {
        QueryWrapper<ParamList> wrapper = new QueryWrapper<ParamList>();
        wrapper.eq("pa_key", key);
        return this.count(wrapper) > 0;
    }

    @Override
    public ParamList addParam(ParamList data) {
        ParamList param = new ParamList();
        param.setPaKey(data.getPaKey());
        param.setPaDesc(data.getPaDesc());
        param.setPaValue(data.getPaValue());
        param.setTypeId(data.getTypeId());
        param.setIsSys(0);
        return this.save(param) ? param : null;
    }

    @Override
    public ParamList updateParam(ParamList data) {
        ParamList param = new ParamList();
        param.setPaId(data.getPaId());
        param.setPaDesc(data.getPaDesc());
        param.setPaValue(data.getPaValue());
        boolean f = this.updateById(param);
        return f ? param : null;
    }


}
